{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "from numpy import *\n",
    "from time import sleep\n",
    "import matplotlib\n",
    "from matplotlib import pyplot as plt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [],
   "source": [
    "def loadfile(filename):\n",
    "    fr=open(filename)\n",
    "    data=[]\n",
    "    for line in fr.readlines():\n",
    "        t=list(map(float,line.strip().split('\\t')))\n",
    "#         print(t)\n",
    "        data.append(t)\n",
    "#     print(data)\n",
    "    return array(data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [],
   "source": [
    "data=loadfile('testSet.txt')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXIAAAD4CAYAAADxeG0DAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjIsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy8li6FKAAAVKElEQVR4nO3db4hsd33H8c93751Fh5sg7r0gmOyMD/rAoIVylyD4oKJVog2GQh9oxyDkwcJVIVJFtAN9UoYigvVBLbK0keAMSKmKRRSr1NInbXGvJmIaLUFm1miLG/MggQ1cw3z7YHeT3dkzs2dmzjm/3++c9wsO3J27O/ObmXM+85vfX3N3AQDStRG6AACA9RDkAJA4ghwAEkeQA0DiCHIASNzVEA96/fp173a7IR4aAJJ1+/bt59z9xuztQYK82+1qf38/xEMDQLLMbJJ1O00rAJA4ghwAEkeQA0DiCHIASBxBDgCJI8gBIHEEOQBJ0mg0Urfb1cbGhrrdrkajUegiISeCHIBGo5F2d3c1mUzk7ppMJtrd3a1VmNf5g8pCrEe+s7PjTAgC4tHtdjWZXJxr0ul0NB6Pqy9Q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VDznu8pM/uwpAclvcvrN0HhWUn3nvn5Hkm/DlSW0plZS8chPnL3r4cuT8neLun9ZvY+Sa+RdLeZDd39Q4HLlRsTgipgZmNJO+4e+8pqKzOzByR9XtIfuvth6PIUzcyu6rgT912SfiXph5L+zN2fClqwEthx7eNxSc+7+8dDl6dKJzXyT7r7g6HLsgzayFGUv5V0l6TvmdkTZval0AUq0klH7sckfVfHnX//WMcQP/F2SQ9LeufJe/nESW0VkaJGDgCJo0YOAIkjyAEgcQQ5ACSOIAeAxBHkAJA4ghwAEkeQA0Di/h9rngaESpihGQAAAABJRU5ErkJggg==\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "def format_pic(data):\n",
    "    plt.scatter(data[:,0],data[:,1],color='k')\n",
    "    plt.show()\n",
    "format_pic(data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4.123105625617661"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def distanceIc(vecA,vecB):\n",
    "    return sqrt(sum(pow(vecA-vecB,2)))\n",
    "distanceIc(array([8,5]),array([4,6]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "matrix([[-1.30358323,  3.02336921],\n",
       "        [ 4.00489318,  0.03280122]])"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#随机产生质心\n",
    "def randcent(data,k):\n",
    "    m,n=data.shape\n",
    "    cents=mat(zeros((k,n)))\n",
    "    for i in range(n):\n",
    "        minJ=min(data[:,i])\n",
    "        maxJ=max(data[:,i])\n",
    "        rangesJ=maxJ-minJ\n",
    "#         print(minJ,maxJ)\n",
    "        cents[:,i]=mat(minJ+rangesJ*random.rand(k,1))\n",
    "    return cents\n",
    "randcent(data,2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
